package com.zyg.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.zyg.common.utils.R;
import com.zyg.user.client.ManagerClient;
import com.zyg.user.entity.TbUser;
import com.zyg.user.service.UserService;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/12/13-14:33
 * ------------------------------
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ManagerClient managerClient;

    // @Autowired
    // private RestTemplate restTemplate;

    @GetMapping({"/", "register.html"})
    public String index(HttpSession session, Model model) {
        //2. 得到session中的数据
        JSONObject user = (JSONObject) session.getAttribute("user");
        // TbUser user = (TbUser) session.getAttribute("user");
        System.out.println("user = " + user);
        //3. 存放到model中
        model.addAttribute("user", user);
        return "register";
    }

    //2. 根据手机号获取验证码
    @GetMapping("/user/getCode")
    public String register(String phone) {

        //1. 根据手机号生成验证码
        userService.createCode(phone);

        return "register";
    }

    //3. 添加用户(需要验证码才行)
    @PostMapping("/user/add")
    public String add(TbUser user, String validcode) {
        System.out.println("user = " + user);
        //3.1 验证验证码是否存在
        boolean b = userService.isExistsCode(validcode, user.getPhone());
        System.out.println("validcode = " + validcode);
        if (b) {
            userService.add(user);
        }
        //应该到登录页面
        return "register";
    }


    //4. 查询zyg-manager微服务的所有品牌
    @GetMapping("user/brand/list")
    @ResponseBody
    public R findAll(HttpServletRequest request) {
        // HttpServletRequest request = ServletActionContext.getRequest();
        //获取用户名（cas登录名的获取方式）
        AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
        String username = principal.getName();
        System.out.println("username = " + username);
        return managerClient.findAll();
    }

    //5. 根据用户id查询用户
    @GetMapping("/user/findById/{id}")
    @ResponseBody
    public R findById(@PathVariable Long id) {
        //5.1 根据用户id查询得到用户
        TbUser user = userService.findById(id);
        //5.2 返回
        return R.ok().put("user", user);
    }

    //6. 根据uid查询tb_user表中的用户是否存在
    @GetMapping("/user/findByUid/{uid}")
    @ResponseBody
    public TbUser findByUid(@PathVariable String uid) {
        return userService.findByUid(uid);
    }

    //7. 添加用户（不需要验证码）
    @PostMapping("/user/insert")
    @ResponseBody
    public R insert(@RequestBody TbUser user) {
        userService.add(user);
        return R.ok();
    }

    //8. 修改用户
    @PostMapping("/user/update")
    @ResponseBody
    public R update(@RequestBody TbUser user) {
        userService.update(user);
        return R.ok();
    }
}
